package com.cal.utms.dao.payment;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cal.utms.entity.CustomerInvestment;
import com.cal.utms.entity.CustomerRedemptionRequest;
import com.cal.utms.entity.CustomerTransactionLog;
import com.cal.utms.entity.MoneyTransaction;
import com.cal.utms.entity.Receipt;
import com.cal.utms.enumType.FundTransferType;
import com.cal.utms.logAudit.AuditLogInterceptor;
import com.cal.utms.util.HibernateUtil;

public class InvestmentDaoImpl implements InvestmentDao {

	public Object addCustomerInvestment(CustomerInvestment customerInvestment) {

		Object obj = new Integer(-1);
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			obj = session.save(customerInvestment);
			tx.commit();
		} catch (Exception ex) {
			try {
				tx.rollback();
			} catch (RuntimeException rbe) {
				rbe.printStackTrace();
			}
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return obj;
	}

	public CustomerInvestment getById(Integer id) {
		Session session = HibernateUtil.getSession().openSession();
		CustomerInvestment customerInvestment;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			customerInvestment = (CustomerInvestment) session.get(CustomerInvestment.class, id);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			customerInvestment = new CustomerInvestment();
		} finally {
			session.close();
		}
		return customerInvestment;
	}

	@SuppressWarnings("unchecked")
	public List<CustomerInvestment> getInvestmentList() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<CustomerInvestment> customerinvestments = new ArrayList<CustomerInvestment>();
		try {
			tx = session.beginTransaction();
			customerinvestments = session.createQuery("from CustomerInvestment").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return customerinvestments;
	}

	public CustomerInvestment getInvesmentByCode(String invenstmentCode) {
		Session session = HibernateUtil.getSession().openSession();
		CustomerInvestment investment = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			investment = (CustomerInvestment) session.createQuery("From CustomerInvestment I where I.invenstmentCode=:code").setParameter("code", invenstmentCode).uniqueResult();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			// session.close();
		}
		return investment;
	}

	public Object updateCustomerInvestment(CustomerInvestment customerInvestment) {
		Object obj = null;
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			obj = session.merge(customerInvestment);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			ex.printStackTrace();
		} finally {
			session.close();
		}
		return obj;
	}

	public Object confirmCustomerInvestment(CustomerInvestment perInvestment, List<MoneyTransaction> moneyTransactions, CustomerRedemptionRequest redemptionRequest,
			Receipt receipt, CustomerRedemptionRequest monthlyRedempRequest, CustomerTransactionLog customerTransactionLog) {
		Object obj = null;
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			obj = session.merge(perInvestment);
			if (moneyTransactions != null) {
				for (MoneyTransaction moneyTransaction : moneyTransactions) {
					session.save(moneyTransaction);
				}
			}
			if (redemptionRequest != null)
				session.save(redemptionRequest);
			if (monthlyRedempRequest != null)
				session.save(monthlyRedempRequest);
			if (customerTransactionLog != null)
				session.save(customerTransactionLog);
			tx.commit();
		} catch (Exception ex) {
			tx.rollback();
			ex.printStackTrace();
		} finally {
			session.close();
		}
		return obj;

	}

	@SuppressWarnings("unchecked")
	public List<CustomerInvestment> getInvestmentByCustomer(int customer_id, int fund_id) {

		Session session = HibernateUtil.getSession().openSession();
		List<CustomerInvestment> investment = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			investment = session.createQuery("From CustomerInvestment I where I.customer.id=:id and I.fund.id=:fundid and I.status=:sta and I.fullRedemption=0")
					.setParameter("id", customer_id).setParameter("fundid", fund_id).setParameter("sta", "CONFIRM").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return investment;
	}

	public void rejectInvestment(CustomerInvestment customerInvestment) {
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.merge(customerInvestment);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public CustomerInvestment getInvesById(int investmentid) {

		Session session = HibernateUtil.getSession().openSession();
		CustomerInvestment customerInvestment = null;
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			customerInvestment = (CustomerInvestment) session.get(CustomerInvestment.class, investmentid);
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return customerInvestment;
	}

	@SuppressWarnings("unchecked")
	public List<CustomerInvestment> getConfirmInvestmentList() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<CustomerInvestment> customerinvestments = new ArrayList<CustomerInvestment>();
		try {
			tx = session.beginTransaction();
			customerinvestments = session.createQuery("from CustomerInvestment C where C.status=:status and C.postStatus=:pstatus").setParameter("status", "CONFIRM")
					.setParameter("pstatus", "NOT POST").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return customerinvestments;
	}

	public void updateInvestment(CustomerInvestment customerInvestment) {
		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.merge(customerInvestment);
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public void cancelInvestment(CustomerInvestment customerInvestment, List<MoneyTransaction> moneyTransactions, List<CustomerRedemptionRequest> redemptions, Receipt receipt,
			CustomerTransactionLog customerTransactionLog) {

		AuditLogInterceptor interceptor = new AuditLogInterceptor();
		Session session = HibernateUtil.getSession().openSession(interceptor);
		interceptor.setSession(session);
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.merge(customerInvestment);
			session.merge(receipt);
			session.save(customerTransactionLog);
			for (MoneyTransaction moneyTransaction : moneyTransactions) {
				session.save(moneyTransaction);
			}
			for (CustomerRedemptionRequest customerRedemptionRequest : redemptions) {
				session.createQuery("UPDATE CustomerRedemptionRequest C SET C.status='CANCEL' WHERE C.id=:id").setParameter("id", customerRedemptionRequest.getId())
						.executeUpdate();
			}
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<CustomerInvestment> getUnConfirmChequeList() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<CustomerInvestment> customerinvestments = new ArrayList<CustomerInvestment>();
		try {
			tx = session.beginTransaction();
			customerinvestments = session.createQuery("from CustomerInvestment C where C.status=:status and C.fundTransferType=:type").setParameter("status", "UNREALISED")
					.setParameter("type", FundTransferType.C).list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return customerinvestments;
	}

	@SuppressWarnings("unchecked")
	public List<CustomerInvestment> getAllInitalInvestmentList() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction tx = null;
		List<CustomerInvestment> customerinvestments = new ArrayList<CustomerInvestment>();
		try {
			tx = session.beginTransaction();
			customerinvestments = session.createQuery("from CustomerInvestment C where C.status=:status").setParameter("status", "INITIAL").list();
			tx.commit();
		} catch (Exception e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return customerinvestments;
	}
}
